package com.boby.homecamera.manager

import android.os.Handler
import android.os.HandlerThread
import com.boby.homecamera.db.VideoEventTable
import com.boby.homecamera.webserver.websocket.MsgEventWebSocketClient
import com.boby.lib_common.BaseApplication
import com.boby.lib_common.util.BLog
import java.io.File
import kotlin.collections.hashMapOf

class ClientMsgWebsocketManager {
    private val clients = hashMapOf<String,MsgEventWebSocketClient>()
    companion object {
        val instance by lazy { ClientMsgWebsocketManager() }
    }
    private lateinit  var handler : Handler
    fun init(){
        val handlerThread = HandlerThread("client_msg_manager")
        handlerThread.start()
        handler = Handler(handlerThread.looper)
    }
    fun add(deviceId:String, ip:String, port:Int,token: String,listener:((videoEvent: VideoEventTable.VideoEvent) -> Unit)? = null){
        handler.post { addInner(deviceId, ip, port,token,listener) }
    }
    @Synchronized
    private fun addInner(deviceId:String, ip:String, port:Int,token: String,listener:((videoEvent: VideoEventTable.VideoEvent) -> Unit)?){
        val client = clients.get(deviceId)
        if(client == null ){
            val client = MsgEventWebSocketClient(deviceId, ip, port,token)
            clients.put(deviceId, client)
            client.setVideoEventListener(listener)
            client.start()
        }else if( client.token != token || client.ip != ip){
            resetTokenInner(deviceId, token, ip,listener)
        }

    }

    fun remove(deviceId:String){
        handler.post { removeInner(deviceId) }
    }


    @Synchronized
    fun removeInner(deviceId:String){
        val client = clients.remove(deviceId)
        client?.close()

    }

    fun resetToken(deviceId:String, token: String, ip:String,listener:((videoEvent: VideoEventTable.VideoEvent) -> Unit)?) {
        handler.post { resetTokenInner(deviceId, token, ip,listener) }
    }
    @Synchronized
    private fun resetTokenInner(deviceId:String, token: String, ip:String,listener:((videoEvent: VideoEventTable.VideoEvent) -> Unit)?){
        val client = clients[deviceId]
        client?.token = token
        client?.ip = ip
        client?.setVideoEventListener(listener)
        client?.reconnectDelay()
    }

    fun sendMsg(deviceId: String,msg: String){
        handler.post { sendMsgInner(deviceId, msg) }
    }
    @Synchronized
    private fun sendMsgInner(deviceId: String,msg: String){
        val client = clients.get(deviceId)
        client?.send(msg)
    }
//    private var testFile : File? = null
     fun sendAudio(deviceId: String,byteArray: ByteArray){
//         if(testFile == null){
//             testFile = File(BaseApplication.instance().externalCacheDir,"test.aac")
//             BLog.d("testFile", "create testFile :${testFile!!.absolutePath}")
//             ///storage/emulated/0/Android/data/com.boby.homecamera/cache/test.aac
//         }
//         testFile?.appendBytes(byteArray)

        val client = clients.get(deviceId)
        client?.sendAudio(byteArray)
    }

}